<template>
    <el-dialog
            :close-on-click-modal="false"
            :model-value="api.editState.visible"
            :title="api.editState.title"
            destroy-on-close
            @close="api.onCloseEdit()"
    >
        <el-form
                ref="formRef"
                :model="api.editState.form"
                :rules="rules"
                label-width="80px"
                status-icon
        >
            <el-space class="mb-3">
                <el-button plain type="primary" @click="addField">增加字段</el-button>
            </el-space>
            <el-table
                    :data="api.editState.form.fields"
                    border
            >

                <el-table-column :width="180" label="类型" prop="type">
                    <template #default="{ row, column, $index }">
                        <el-select v-if="api.editState.form.fields" v-model="api.editState.form.fields[$index].type">
                            <el-option label="String" value="java.lang.String"/>
                            <el-option label="Long" value="java.lang.Long"/>
                            <el-option label="Integer" value="java.lang.Integer"/>
                            <el-option label="Boolean" value="java.lang.Boolean"/>
                            <el-option label="BigInteger" value="java.math.BigInteger"/>
                            <el-option label="Float" value="java.lang.Float"/>
                            <el-option label="Double" value="java.lang.Double"/>
                            <el-option label="Short" value="java.lang.Short"/>
                            <el-option label="BigDecimal" value="java.math.BigDecimal"/>
                            <el-option label="Date" value="java.util.Date"/>
                            <el-option label="Timestamp" value="java.sql.Timestamp"/>
                            <el-option label="Time" value="java.sql.Time"/>
                            <el-option label="long" value="long"/>
                            <el-option label="int" value="int"/>
                            <el-option label="boolean" value="boolean"/>
                            <el-option label="float" value="float"/>
                            <el-option label="double" value="double"/>
                            <el-option label="short" value="short"/>
                            <el-option label="char" value="char"/>
                        </el-select>
                    </template>
                </el-table-column>

                <el-table-column label="名称" prop="name">
                    <template #default="{ row, column, $index }">
                        <el-input
                                v-if="api.editState.form.fields"
                                v-model="api.editState.form.fields[$index].name"
                                clearable
                                placeholder="名称"
                        />
                    </template>
                </el-table-column>

                <el-table-column :width="80" label="exist" prop="exist">
                    <template #default="{ row, column, $index }">
                        <el-switch v-if="api.editState.form.fields" v-model="api.editState.form.fields[$index].exist"/>
                    </template>
                </el-table-column>

                <el-table-column label="备注" prop="commentComment">
                    <template #default="{ row, column, $index }">
                        <el-input
                                v-if="api.editState.form.fields"
                                v-model="api.editState.form.fields[$index].commentComment"
                                clearable
                                placeholder="备注"
                        />
                    </template>
                </el-table-column>

                <el-table-column align="center" label="删除" prop="action" width="70">
                    <template #default="{ row, column, $index }">
                        <el-button
                                size="small"
                                text
                                type="danger"
                                @click="onDeleteField($index)"
                        >
                            <template #icon>
                                <el-icon>
                                    <Delete/>
                                </el-icon>
                            </template>
                        </el-button>
                    </template>
                </el-table-column>

            </el-table>

            <el-form-item class="mb-0 mt-4">
                <el-button type="primary" @click="onSubmit">保存</el-button>
                <el-button @click="api.onCloseEdit()">关闭</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script lang="ts" setup>
import {GenTableApi} from "~/api/system/gen/gen.api";
import {reactive, ref} from "vue";
import {FormRules} from "element-plus";
import {Delete} from "@element-plus/icons-vue";

interface Props {
    api: GenTableApi;
}

const formRef = ref();
const {api} = withDefaults(defineProps<Props>(), {});
const rules = reactive<FormRules>({
    packageName: [{required: true, message: "请输入包路径", trigger: "blur"}],
    tableName: [{required: true, message: "请输入表名", trigger: "blur"}],
    tableComment: [{required: true, message: "请输入表描述", trigger: "blur"}],
    moduleName: [{required: true, message: "请输入模块名", trigger: "blur"}],
    className: [{required: true, message: "请输入类名", trigger: "blur"}],
    requestMapping: [{required: true, message: "请输入requestMapping", trigger: "blur"}],
});

function addField() {
    if (!api.editState.form.fields) {
        api.editState.form.fields = [];
    }
    api.editState.form.fields.push({
        type: "String",
        name: "test",
        exist: true,
        commentComment: "备注",
    });
}

async function onDeleteField(index: number) {
    api.editState.form.fields?.splice(index, 1);
}

async function onSubmit() {
    const values = await formRef.value.validate();
    if (values) {
        api.submit(api.editState.form).then((res: any) => {
            if (res.ok) {
                api.onCloseEdit();
            }
        });
    }
}
</script>

<style lang="scss" scoped>

</style>
